<?php
/**
 * @version SVN: $Id: install.easycreator.php 1221 2010-05-31 19:00:50Z elkuku $
 * @package    EasyCreator
 * @subpackage Installer
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath (elkuku) {@link http://www.nik-it.de NiK-IT.de}
 * @author     Created on 30-Sep-2008
 * @license    GNU/GPL, see JROOT/LICENSE.php
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

defined('NL') or define('NL', "\n");

define('ECR_XML_LOCATION', $this->parent->getPath('manifest'));

/**
 * Main installer.
 */
function com_install()
{
    $PHPMinVersion = '5.2.4';

    if(version_compare(PHP_VERSION, $PHPMinVersion, '<'))
    {
        JError::raiseWarning(0, JText::sprintf('This script requires at least PHP version %s', $PHPMinVersion));

        return false;
    }

    if( ! $xml = simplexml_load_file(ECR_XML_LOCATION))
    {
        JError::raiseWarning(0, JText::_('Install manifest not found'));

        return false;
    }

    define('ECR_VERSION', $xml->version);

    require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_easycreator'.DS.'helpers'.DS.'html.php';

    JFactory::getLanguage()->load('com_easycreator');

    JFactory::getDocument()->addStyleSheet(JURI::root().'administrator/components/com_easycreator/assets/css/default.css');
    JFactory::getDocument()->addStyleSheet(JURI::root().'administrator/components/com_easycreator/assets/css/icon.css');
    ?>

<div>

<div style="float: right">
	<img
	src="<?php echo JURI::root(); ?>administrator/components/com_easycreator/assets/images/easycreator-shadow.png"
	alt="EasyCreator Logo" title="EasyCreator Logo" />
</div>

<h1>EasyCreator</h1>
<?php echo JText::_('ECR_INSTALL_DESC'); ?>

<p>
Happy coding,<br />
The <a href="http://www.easy-joomla.org">Easy-Joomla.org</a> Team.
</p>

</div>

<h3 style="color: orange;"><?php echo JText::_('ECR_INSTALL_WARN_LOCAL_DEV'); ?></h3>
<p>
<?php echo JText::_('ECR_INSTALL_JDOC_LINK'); ?>
</p>

<?php

    /*
     * MD5 check
     */
    jimport('joomla.filesystem.file');

    $paths = array(
      'admin' => JPATH_ADMINISTRATOR.DS.'components'.DS.'com_easycreator'
    , 'site' => JPATH_SITE.DS.'components'.DS.'com_easycreator');

    $md5Path = $paths['admin'].DS.'MD5SUMS';

    if(JFile::exists($md5Path))
    {
        echo '<br />'.JText::_('Checking MD5 sums...');

        $md5Check = checkMD5File($md5Path, $paths);

        if(count($md5Check))
        {
            echo '<strong style="color: red;">'.JText::_('There have been errors').'</strong>';
            echo '<ul style="color: red;">';
            echo '<li>';
            echo implode('</li><li>', $md5Check);
            echo '</li>';
            echo '</ul>';
        }
        else
        {
            echo '<strong style="color: green;">OK</strong>';
        }
    }

    ecrHTML::footer();

    return true;
}//function

/**
 * Checks an extension against a given MD5 checksum file.
 *
 * @param string $path Path to md5 file
 * @param array $extensionPaths Indexed array:
 *         First folder in md5 file path as key - extension path as value
 *         e.g. array(
 *            'admin' => JPATH_ADMINISTRATOR.DS.'components'.DS.'com_easycreator'
 *          , 'site' => JPATH_SITE.DS.'components'.DS.'com_easycreator');
 *
 * @return array Array of errors
 */
function checkMD5File($path, $extensionPaths)
{
    jimport('joomla.filesystem.file');

    $lines = explode("\n", JFile::read($path));

    $errors = array();

    foreach ($lines as $line)
    {
        if( ! trim($line)) continue;

        list($md5, $file) = explode(' ', $line);

        $parts = explode(DS, $file);

        if( ! array_key_exists($parts[0], $extensionPaths)) continue;

        $path = $extensionPaths[$parts[0]].DS.substr($file, strlen($parts[0]) + 1);

        if(JFile::exists($path))
        {
            if(md5_file($path) != $md5)
            {
                $errors[] = JText::sprintf('MD5 check failed on file: %s', $path);
            }
        }
        else
        {
            $errors[] =  JText::sprintf('File not found: %s', $path);
        }
    }//foreach

    return $errors;
}//function
